#ifndef PW_PBL_COROUTINE_HPP
#define PW_PBL_COROUTINE_HPP

#include "Coroutine/Core.hpp"
#include "Coroutine/Awaitor.hpp"

namespace pw_pbl
{
	namespace _co
	{
		template <typename E>
		concept ccExecutor = requires(E &excutor, typename E::IAwaitable &awaitor) {
			E{};
			excutor.await_suspend(awaitor);
		};

		/// 协程执行器工厂类
		template <ccExecutor T>
		class ExecutorFactory
		{
			static T Instance;
			static thread_local T tlsInstance;

		public:
			consteval static T &instance()
			{
				if constexpr (co::using_thread_local_executor)
					return tlsInstance;
				else
					return Instance;
			}
		};

		template <ccExecutor T>
		T ExecutorFactory<T>::Instance{};

		template <ccExecutor T>
		thread_local T ExecutorFactory<T>::tlsInstance{};
	} // namespace _co
} // namespace pw_pbl

namespace pw_pbl::co
{
	/// 获取协程执行器考虑各种配置项后最终选择的执行器实例，用户使用时建议使用executor()表示
	/// @note 最终实例与模板参数T的类型无关，模板参数T只用于延迟实例化
	template <typename T>
	inline consteval auto &executor() { return _co::ExecutorFactory<co::Executor>::instance(); };
} // namespace pw_pbl::co

#endif // PW_PBL_COROUTINE_HPP
